package cn.com.dhcc.turbo.service.exch.cmdata.validator;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.util.CollectionUtil;
import cn.com.dhcc.app.core.util.Status;
import cn.com.dhcc.app.core.util.StrUtil;
import cn.com.dhcc.app.core.util.XstreamUtil;
import cn.com.dhcc.app.pub.core.consts.AppIocBean;
import cn.com.dhcc.app.pub.core.consts.NodeInfoParam;
import cn.com.dhcc.app.pub.core.tag.AppTagEl;
import cn.com.dhcc.turbo.entity.api.Posthead;
import cn.com.dhcc.turbo.entity.api.RouteEntry;
import cn.com.dhcc.turbo.entity.registry.AdapterInfo;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
import cn.com.dhcc.turbo.service.registry.AdapterInfoService;
import cn.com.dhcc.turbo.service.registry.BaseInfoService;
/**
 * posthead报文校验器
 */
@Service(AppIocBean.POSTHEAD_VALIDATOR_BEAN)
public class PostheadValidator implements MessageValidator {
	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getDetachLogger(LOGTYPE.TURBO);
	
	
	@Autowired
	@Qualifier(AppIocBean.BASE_INFO_SERVICEIMPL_BEAN)
	private BaseInfoService baseInfoService;

	

	
	@Autowired
    private AdapterInfoService adapterInfoService;
	

	/**
	 * 校验posthead
	 * @param params
	 * @return
	 */
	@Override
	public Status validate(Object... params) {
		String msg = "";
		Status status = Status.getInstance();
        String postheadXml = (String)params[0];
        String data = (String)params[1];
		try {
			NodeInfo localNode=baseInfoService.getCurrentNodeInfo();
			if (localNode==null) {
				msg = "节点还未完成初始化，请稍后再发送数据..";
				status.setSuccess(false);
				status.setMsg(msg);
				return status;
			}
			if (StrUtil.isBlank(postheadXml)) {
				msg = "pohsthead参数不能为空";
				status.setSuccess(false);
				status.setMsg(msg);
				return status;
			}
			String payload = StrUtil.fixNull(data);
			if (StrUtil.isBlank(payload)) {
				msg = "data参数不能为空";
				status.setSuccess(false);
				status.setMsg(msg);
				return status;
			}
			postheadXml = postheadXml.trim();
			Posthead posthead = XstreamUtil.toObject(Posthead.class,postheadXml);
			/*************非空验证**************/
			//验证头信息
			if (posthead == null) {
				msg = "解析pohsthead头信息出错";
				status.setSuccess(false);
				status.setMsg(msg);
				return status;
			}
			
			//from非空验证
			if (posthead.getFrom() == null) {
				msg = "解析pohsthead头信息出错，发送信息posthead/from不能为空";
				status.setSuccess(false);
				status.setMsg(msg);
				return status;
			}
			//from/nodecode非空验证
			if (StrUtil.isBlank(posthead.getFrom().getNodecode())) {
				msg = "解析pohsthead头信息出错，发送头信息中posthead/from/nodecode参数不能为空";
				status.setSuccess(false);
				status.setMsg(msg);
				return status;
			}
			//from/adaptorcode非空验证
			if (StrUtil.isBlank(posthead.getFrom().getAdaptorcode())) {
				msg = "解析pohsthead头信息出错，发送头信息中posthead/from/nodecode参数不能为空";
				status.setSuccess(false);
				status.setMsg(msg);
				return status;
			}
			
           //to非空验证
			if (!CollectionUtil.hasElement(posthead.getToList())) {
				msg = "解析pohsthead头信息出错，接收信息posthead/to不能为空";
				status.setSuccess(false);
				status.setMsg(msg);
				return status;
			}
			
			
			//to/nodecode非空验证
			for (RouteEntry to : posthead.getToList()) {
				if (StrUtil.isBlank(to.getNodecode())) {
					msg = "解析pohsthead头信息出错，接收信息中posthead/to/nodecode参数不能为空";
					status.setSuccess(false);
					status.setMsg(msg);
					return status;
				}
				
				if (to.getNodecode().length()!=posthead.getFrom().getNodecode().length()) {
					msg = "解析pohsthead头信息出错，接收信息中posthead/to/nodecode参数格式不正确";
					status.setSuccess(false);
					status.setMsg(msg);
					return status;
				}
				
				
			}
			
			
			//校验适配器
			AdapterInfo adapterInfo=adapterInfoService.getAdapterInfo(posthead.getFrom().getNodecode(), posthead.getFrom().getAdaptorcode());
			if (adapterInfo==null) {
				msg = "校验pohsthead头信息失败，发送适配器["+posthead.getFrom().getAdaptorcode()+"]未注册";
				status.setSuccess(false);
				status.setMsg(msg);
				return status;
			}
			
			//校验发送节点
			NodeInfo nodeInfo=baseInfoService.getNodeInfoByCode(posthead.getFrom().getNodecode());
			if (nodeInfo==null) {
				msg = "校验pohsthead头信息失败，发送系统["+posthead.getFrom().getNodecode()+"]未在平台注册";
				status.setSuccess(false);
				status.setMsg(msg);
				return status;
			}
			if (!nodeInfo.getNodeCode().equals(localNode.getNodeCode())) {
				msg = "校验pohsthead头信息失败，发送节点["+posthead.getFrom().getNodecode()+"]不是当前节点";
				status.setSuccess(false);
				status.setMsg(msg);
				return status;
			}
			
			//判断是否是内网到外网传输
			if (NodeInfoParam.isInnerNode()) {//from 节点时内网
				boolean flag=false;
				for (RouteEntry to : posthead.getToList()) {
					if (!AppTagEl.isInnerNet(to.getNodecode().substring(6, 7))) {//如果to为外网
						flag=true;
						break;
					}
				}
				if (flag) {//如果有内网到外网
					//from/auditor非空验证
					if (StrUtil.isBlank(posthead.getFrom().getAuditor())) {
						msg = "解析pohsthead头信息出错，发送头信息中posthead/auditor参数不能为空";
						status.setSuccess(false);
						status.setMsg(msg);
						return status;
					}
				}
			}

			
			//如果能查到nodecode说明，非跨网传输，否则跨网传输
			for (int i=0;i<posthead.getToList().size();i++) {
				RouteEntry to = posthead.getToList().get(i);
				if (posthead.getFrom().getNodecode().substring(6, 7).equals(to.getNodecode().substring(6, 7))) {//同网段的必须判断接收节点也存在
					NodeInfo toNodeInfo=baseInfoService.getNodeInfoByCode(to.getNodecode());
					if (toNodeInfo==null) {
						msg = "校验pohsthead头信息失败，接收节点["+to.getNodecode()+"]未在平台注册";
						status.setSuccess(false);
						status.setMsg(msg);
						return status;
					}
					
					//判断接收适配器是否存在：
					AdapterInfo info=baseInfoService.getAdapterInfo(to.getNodecode(), adapterInfo.getAdapterCode());
					if (info==null) {
						msg = "校验pohsthead头信息失败，接收节点["+to.getNodecode()+"]未绑定适配器["+adapterInfo.getAdapterCode()+"],请通知接收方绑定该适配器";
						status.setSuccess(false);
						status.setMsg(msg);
						return status;
					}
				}
			}
			msg = "校验成功";
			status.setSuccess(true);
			status.setMsg(msg);
			status.setData(posthead);
			return status;
		} catch (Exception e) {
			msg = "解析pohsthead头信息出错:" + postheadXml + ">>>cause:" + e.fillInStackTrace();
			logger.error(msg, e);
			status.setSuccess(false);
			status.setMsg(msg);
			return status;
		}
	}
}
